const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue: "",
        hadPayList: [],
        isLastPage: false,
        current: 1,
        size: 10,
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
        showBookPicker: false,
        bookList: [],
        syncOrderItem: {},
    },

    onLoad: function (options) {
        this.setData({
            userId: options.id,
        })
        wx.setNavigationBarTitle({
            title: options.title
        })
        this.getData()
        this.getBookData()
    },

    onPullDownRefresh() {
        this.setData({
            current: 1,
            searchValue: "",
            hadPayList: [],
            isLastPage: false
        })
        this.getData()
    },
    onReachBottom() {
        let {
            current,
            isLastPage
        } = this.data
        if (isLastPage) {
            return
        }
        this.setData({
            current: (Number(current) + 1)
        })
        this.getData()
    },
    onSearch() {
        this.setData({
            current: 1,
            hadPayList: [],
            isLastPage: false
        })
        this.getData()
    },
    toDetail(e) {
        let item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: `/pages/business/details/index?id=${item.id}`,
        })
    },
    getBookData() {
        wx.$apis.getAccountBookEasy().then(res => {
            let bookList = res.data && res.data.map(item => {
                item.text = item.name
                return item
            })
            this.setData({
                bookList: bookList
            })
        })
    },
    syncToAccountBook(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            syncOrderItem: item,
            showBookPicker: true
        })
    },
    onBookCancel() {
        this.setData({
            syncOrderItem: {},
            showBookPicker: false
        })
    },
    onBookConfirm(event) {
        const {
            value
        } = event.detail;
        let params = {
            accountBookId: value.id,
            financeName: "买菜",
            recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
            orderType: 2,
            orderId: this.data.syncOrderItem.id,
            type: 1,
        }
        wx.showLoading({
            title: "正在同步"
        })
        wx.$apis.addAccountBookRecord(params).then(res => {
            this.setData({
                showBookPicker: false
            })
            wx.showToast({
                title: '同步完成！',
                icon: 'success',
                duration: 1500
            })
            this.onPullDownRefresh()
        })
    },
    getData() {
        const {
            current,
            searchValue,
            size
        } = this.data;
        let params = {
            current,
            size,
            cropName: searchValue,
            enterStockStatus: 4,
            companyId: app.globalData.userInfo.companyData.companyId,
            farmerUserId: this.data.userId
        }
        wx.showLoading({
            title: "正在请求数据中"
        })
        wx.$apis.getMarketList(params).then(res => {
            wx.stopPullDownRefresh();
            let oldHadPayList = this.data.hadPayList;
            let hadPayList = res.data && res.data.records.map(item => {
                item.createTimeStr = item.createTime && item.createTime.replace("T", " ")
                item.countPrice = item.countPrice && wx.$getYuan(item.countPrice)
                item.managementFee = item.managementFee && wx.$getYuan(item.managementFee)
                item.transactionPrice = item.transactionPrice && wx.$getYuan(item.transactionPrice)
                return item
            })
            if (hadPayList && hadPayList.length) {
                let newHadPayList = oldHadPayList.concat(hadPayList)
                this.setData({
                    hadPayList: newHadPayList
                })
                if (hadPayList.length < size) {
                    this.setData({
                        isLastPage: true
                    })
                }
            } else {
                this.setData({
                    isLastPage: true
                })
            }
        }).catch(err => {
            wx.stopPullDownRefresh();
        })
    }
})